package com.ciban.smarthome.module.ui;

import java.util.ArrayList;

import com.ciban.smarthome.R;
import com.ciban.smarthome.module.BaseApplication;
import com.ciban.smarthome.module.ui.base.BaseFragment;
import com.ciban.smarthome.module.engine.AreaEngine;
import com.ciban.smarthome.module.entity.Area;
import com.ciban.smarthome.module.ui.adapter.AreaGridAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AreraFragment extends BaseFragment {
    private static final int INTENT_MODIFY_AREAS = 100;
    private static final int INTENT_AREA_DETAILS = 101;

    private ImageView iv_add;
    private GridView gv_content;

    private AreaGridAdapter adapter;
    private ArrayList<Area> areasList;
    private ArrayList<Area> tmpDelList;
    private boolean isModify = false;


    private AreaEngine engine;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        initData();
        View view = inflater.inflate(R.layout.fragment_area, null);
        findViews(view);
        return view;
    }

    /**
     * 初始化数据
     */
    private void initData() {
        engine = new AreaEngine(getActivity());
        areasList = engine.getAreaList();
        //在这应该判断是否第一次进入系统
        if (areasList.size() == 0)
        {
            engine.initDbContent();
            areasList = engine.getAreaList();
        }
    }

    /**
     * 刷新区域的显示
     */
    private void refreshAreaShow() {
        if (adapter == null) {
            adapter = new AreaGridAdapter(getActivity(), areasList);
            gv_content.setAdapter(adapter);
        } else {
            engine.refreshAreaList();
            adapter.notifyDataSetChanged();
        }
    }

    private void findViews(View view) {
        ((TextView) view.findViewById(R.id.title)).setText(R.string.area);
        gv_content = (GridView) view.findViewById(R.id.area_gv_content);
        iv_add = (ImageView) view.findViewById(R.id.title_add);

        setListeners();

        refreshAreaShow();

    }

    private void setListeners() {
        gv_content.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                Area info = areasList.get(position);
                if (isModify) {
                    if (tmpDelList == null)
                        tmpDelList = new ArrayList<Area>();

                    info.setIsSel(!info.isSel());
                    if (info.isSel()) {
                        tmpDelList.add(info);
                    } else {
                        tmpDelList.remove(info);
                    }
                    adapter.notifyDataSetChanged();
                } else {

                    ((BaseApplication) getActivity().getApplication())
                            .setNowArea(info);

                    Intent intent = new Intent(getActivity(),
                            AreaDetailsActivity.class);
                    intent.putExtra("area", info);
                    startActivityForResult(intent, INTENT_AREA_DETAILS);
                }
            }
        });
        iv_add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isModify)
                    return;
                showMenuWin(iv_add);
            }
        });
    }

    @Override
    public boolean delItem() {

        if (tmpDelList.size() > 0) {

            for (Area info : tmpDelList) {
                engine.delArea(info.getId());
            }
            areasList.removeAll(tmpDelList);

            isModify = false;
            refreshLv();

            tmpDelList.clear();
            tmpDelList = null;
        } else {
            Toast.makeText(getActivity(), "请选中", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void delCancle() {
        for (Area info : areasList) {
            info.setIsSel(false);
        }
        isModify = false;
        refreshLv();
    }


    @Override
    protected void addMode() {
        super.addMode();
        menuPopWin.dismiss();
        Intent intent = new Intent(getActivity(), AreaModifyActivity.class);
        startActivityForResult(intent, INTENT_MODIFY_AREAS);
    }

    @Override
    protected void editMode() {
        super.editMode();
        menuPopWin.dismiss();
        ((MainActivity) getActivity()).showDelView();
        isModify = true;
        refreshLv();
    }

    private void refreshLv() {
        adapter.setModify(isModify);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case INTENT_MODIFY_AREAS:
                if (resultCode == Activity.RESULT_OK) {
                    refreshAreaShow();
                }
                break;
            case INTENT_AREA_DETAILS:
                if (resultCode == Activity.RESULT_OK) {
                    refreshAreaShow();
                }
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        engine.destory();
    }
}
